<?php

namespace Modules\Common\Http\Controllers;

use Exception;
use Modules\Common\Repository\Options\Factory;

class OptionController
{
    /**
     * @param $name
     * @param Factory $factory
     * @return array
     * @throws Exception
     */
    public function index($name, Factory $factory): array
    {

        $enumClass = "\App\Enums\\{$name}";
        if(enum_exists($enumClass)){
            return $this->get($enumClass);
        }else{
            return $factory->make($name)->get();
        }

    }

    /**
     * 枚举数据转换
     * @param $enumClass
     * @return array
     */
    private function get($enumClass): array
    {
        $list = [];
        foreach ($enumClass::cases() as $case){
            $list[] = [
                'value' => $case->value,
                'label' => $case->label()
            ];
        }
        return $list;
    }

}
